/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import Model.Book;

import Model.Category;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Admin
 */
public class UpdateBook extends javax.swing.JDialog {

    /**
     * Creates new form UpdateBook
     */
    ObjectOutputStream out;
    ObjectInputStream in;
    static Book book;

    public UpdateBook(java.awt.Frame parent, boolean modal, Book book, ObjectOutputStream out, ObjectInputStream in) {
        super(parent, modal);
        UpdateBook.book = book;
        this.in = in;
        this.out = out;
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            //  UIManager.put("Button.showMnemonics",Boolean.TRUE);
        } catch (InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        initComponents();
        loadDataCategory();
        txtBookID.setText(book.BookID);
        txtTitle.setText(book.Title);
        txtSumary.setText(book.Sumary);
        txtAuthor.setText(book.Author);
        txtPublisher.setText(book.Publisher);
        CategoryClient cate = new CategoryClient();
        cbCategory.setSelectedIndex((Integer.parseInt(book.CategoryID)));
    }

    private void loadDataCategory() {
        try {
            CategoryClient cate = new CategoryClient();
            ArrayList<String> arrString = new ArrayList<>();
            out.writeObject("getCategoryName");
            arrString = (ArrayList) in.readObject();

            String[] s = new String[arrString.size()];
            arrString.toArray(s);
            DefaultComboBoxModel model = new DefaultComboBoxModel(s);
            cbCategory.setModel(model);
        } catch (ClassNotFoundException | IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblUpdateBook = new javax.swing.JLabel();
        pnInformation = new javax.swing.JPanel();
        lblBookID = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        lblAuthor = new javax.swing.JLabel();
        lblPublisher = new javax.swing.JLabel();
        lblCategory = new javax.swing.JLabel();
        lblSumary = new javax.swing.JLabel();
        txtBookID = new javax.swing.JTextField();
        txtTitle = new javax.swing.JTextField();
        txtAuthor = new javax.swing.JTextField();
        txtPublisher = new javax.swing.JTextField();
        cbCategory = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtSumary = new javax.swing.JTextArea();
        btnNewCategoryUpdate = new javax.swing.JButton();
        btnEditCategory = new javax.swing.JButton();
        pnButton = new javax.swing.JPanel();
        btnUpdate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Update Book");
        setBackground(new java.awt.Color(240, 240, 240));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        lblUpdateBook.setFont(new java.awt.Font("Arial", 1, 24));
        lblUpdateBook.setText("Update Book");

        pnInformation.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        lblBookID.setText("Book ID");

        lblTitle.setText("Title");

        lblAuthor.setText("Author");

        lblPublisher.setText("Publisher");

        lblCategory.setText("Category");

        lblSumary.setText("Sumary");

        txtBookID.setEditable(false);

        txtSumary.setColumns(20);
        txtSumary.setLineWrap(true);
        txtSumary.setRows(5);
        jScrollPane1.setViewportView(txtSumary);

        btnNewCategoryUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonNew16px.png"))); // NOI18N
        btnNewCategoryUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewCategoryUpdateActionPerformed(evt);
            }
        });

        btnEditCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/buttonEdit16px.png"))); // NOI18N
        btnEditCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditCategoryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnInformationLayout = new javax.swing.GroupLayout(pnInformation);
        pnInformation.setLayout(pnInformationLayout);
        pnInformationLayout.setHorizontalGroup(
            pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInformationLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnInformationLayout.createSequentialGroup()
                        .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnInformationLayout.createSequentialGroup()
                                .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblAuthor)
                                    .addComponent(lblTitle))
                                .addGap(22, 22, 22)
                                .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnInformationLayout.createSequentialGroup()
                                .addComponent(lblBookID)
                                .addGap(18, 18, 18)
                                .addComponent(txtBookID, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(51, 51, 51)
                        .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblCategory)
                            .addComponent(lblPublisher))
                        .addGap(18, 18, 18)
                        .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPublisher)
                            .addComponent(cbCategory, 0, 134, Short.MAX_VALUE)))
                    .addGroup(pnInformationLayout.createSequentialGroup()
                        .addComponent(lblSumary)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNewCategoryUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditCategory)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnInformationLayout.setVerticalGroup(
            pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInformationLayout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnEditCategory)
                    .addComponent(btnNewCategoryUpdate)
                    .addGroup(pnInformationLayout.createSequentialGroup()
                        .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblBookID)
                            .addComponent(lblCategory)
                            .addComponent(txtBookID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(42, 42, 42)
                        .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTitle)
                            .addComponent(lblPublisher)
                            .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPublisher, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36)
                        .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblAuthor)
                            .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(41, 41, 41)
                        .addGroup(pnInformationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSumary)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonUpdate32px.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        pnButton.add(btnUpdate);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ButtonCancel.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        pnButton.add(btnCancel);

        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/LogoDVT.gif"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addComponent(pnInformation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(222, 222, 222)
                        .addComponent(lblUpdateBook)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 192, Short.MAX_VALUE)
                .addComponent(pnButton, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(jLabel10)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(lblUpdateBook)
                .addGap(18, 18, 18)
                .addComponent(pnInformation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(pnButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(33, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnNewCategoryUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewCategoryUpdateActionPerformed

        // TODO add your handling code here:
        dialogNewCategory category = new dialogNewCategory(null,true,out,in);
        category.setLocationRelativeTo(this);
        category.setVisible(true);
        loadDataCategory();

}//GEN-LAST:event_btnNewCategoryUpdateActionPerformed

private void btnEditCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditCategoryActionPerformed
    // TODO add your handling code here:
        Category oldCate = new Category(Integer.toString(cbCategory.getSelectedIndex()),(String)cbCategory.getSelectedItem());
        dialogEditCategory category = new dialogEditCategory(null,true,oldCate,out,in);
        category.setLocationRelativeTo(this);
        category.setVisible(true);
        loadDataCategory();
}//GEN-LAST:event_btnEditCategoryActionPerformed

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
// TODO add your handling code here:
    this.setVisible(true);
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
// TODO add your handling code here:
    book.Author = txtAuthor.getText();
    book.Publisher = txtPublisher.getText();
    book.Sumary = txtSumary.getText();
    book.Title = txtTitle.getText();
    //book.UnitPrice=Integer.parseInt(txtUnitPrice.getText());
    CategoryClient cate = new CategoryClient();
    book.CategoryID = Integer.toString(cbCategory.getSelectedIndex());
    try {
        out.writeObject("editBook");
        out.writeObject(book);//goi doi tuong sinh vien cho sever        
        boolean alert = (boolean) in.readObject();//nhan thong bao tu server    
        if (alert) {
            JOptionPane.showMessageDialog(this, "Update book success");
            this.setVisible(false);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Update failed");
        }
    } catch (ClassNotFoundException | IOException ex) {
        System.out.println(ex);
    }

}//GEN-LAST:event_btnUpdateActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    try {
        // TODO add your handling code here:
        in.close();
        out.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}//GEN-LAST:event_formWindowClosing
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnEditCategory;
    private javax.swing.JButton btnNewCategoryUpdate;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbCategory;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAuthor;
    private javax.swing.JLabel lblBookID;
    private javax.swing.JLabel lblCategory;
    private javax.swing.JLabel lblPublisher;
    private javax.swing.JLabel lblSumary;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblUpdateBook;
    private javax.swing.JPanel pnButton;
    private javax.swing.JPanel pnInformation;
    private javax.swing.JTextField txtAuthor;
    private javax.swing.JTextField txtBookID;
    private javax.swing.JTextField txtPublisher;
    private javax.swing.JTextArea txtSumary;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
}
